<?php

namespace App\Http\Controllers;

use App\Models\File;
use App\Utils\CodeUtil;
use App\Utils\ConstantUtil;
use Illuminate\Http\Request;
//use zgldh\QiniuStorage\QiniuStorage;

class UploadController extends Controller
{

    public function upload(Request $request ,$name="image") {
        $file =  $request->file($name);
        $type = $file->getMimeType();
        $file_name = $file->getClientOriginalName();
        $data = $request->all();
        $image = ["png","jpg","jpeg","gif","bmp","svg","psd"];
        $files = ["doc","docx","xlsx","xls","txt","pdf"];
        $video = ["mp4","mov"];
        // 缺少需要控制大小
        // 缺少格式验证
        if($file){
            $fileObj = new File();
            $file_type = $file->getClientOriginalExtension();
            $size = $file->getClientSize();
            if (!empty($data['id'])) {
                unset($data['id']);
                unset($data['image']);
            }
            if(in_array($file_type,$image)) {
                $fileObj->type = ConstantUtil::FILES_TYPE_IMAGE;
            }elseif(in_array( $file_type , $files)){
                $fileObj->type = ConstantUtil::FILES_TYPE_FILE;
            }elseif(in_array( $file_type , $video)){
                $fileObj->type = ConstantUtil::FILES_TYPE_VIDEO;
            }else{
                return (['code'=> CodeUtil::API_ERROR,"msg"=> "该格式不支持"]);
            }
            /**
             * 七牛上传
             */
            /*
            $disk = QiniuStorage::disk('qiniu');
            $fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension();
            $fileName = "image/".$fileName;
            $bool = $disk->put($fileName,file_get_contents($file->getRealPath()));
            if(empty($bool)) return ['code'=> CodeUtil::API_ERROR,"msg"=> "上传失败"];
            $path = $disk->downloadUrl($fileName);
            $fileObj->name = $file_name;
            $fileObj->uri  = $fileName;
            $fileObj->mime = $type;
            $fileObj->size = $size;
            $fileObj->save();
              return (['code'=> CodeUtil::SUCCESS,"msg"=> "上传成功","data" => ["id"=>$fileObj->id,"url"=>config("filesystems.uri")."/".$fileName,"name"=>$file_name]]);
            */
            /**
             * 普通上传
             */
            $fileName = md5($file->getClientOriginalName().time().rand()).'.'.$file->getClientOriginalExtension();
            $base_path = base_path()."/public";
            $year = date("Y");
            //$year_file = '/upload/image/'.$year;
            $month = date("m");
            //$month_file = '/upload/image/'.$year.'/'.$month;
            $day = date("d");
            $path = '/upload/image/'.$year."/".$month."/".$day;
            $file -> move($base_path.$path,$fileName);
            $fileObj->name = $file_name;
            $fileObj->uri  = $path."/".$fileName;
            $fileObj->mime = $type;
            $fileObj->size = $size;
            $fileObj->save();
            return (['code'=> CodeUtil::SUCCESS,"msg"=> "上传成功","data" => ["id"=>$fileObj->id,"url"=>$path."/".$fileName,"name"=>$file_name]]);
        }else{
            return (['code'=> CodeUtil::API_ERROR,"msg"=> "上传图片文件为空"]);
        }
    }
}
